<?php
class Users_RegisterController extends Zend_Controller_Action
{
	private $ns_register = null;

	public function init()
    {
        /* Initialize action controller here */
		$this->ns_register = new Zend_Session_Namespace('register');
    }

    public function indexAction()
    {
		if($this->getRequest()->isPost()) {
			$formData = $this->getRequest()->getPost();
			$this->ns_register->type = $formData['type'];
			$this->_redirect('/users/register/pasul-doi');
		}
		
    }

    public function pasulDoiAction()
    {

		$form = new Application_Form_RegisterForm( array('action'=>'/users/register/pasul-doi') );

		if($this->getRequest()->isPost()) {
			$formData = $this->getRequest()->getPost();
			if ( $form->isValid($formData) ){
				//save
				$modelUser = new Application_Model_Users();
				$formData['salt'] = $modelUser->generateSalt();
				$data = array(
					'email' => $formData['email'], 'password' => md5(md5($formData['password']).$formData['salt']),
					'username' => $formData['username'], 'salt' => $formData['salt']
				);
				$userID = $modelUser->saveUser( $data );

				$_properties = Zend_Registry::get('_PROPERTIES');

				switch($this->ns_register->type) {
					case 'companie':
						$modelUserCompany = new Application_Model_UserCompanies();
						$userCompany = array(
							'user_id'	=> $userID,
							'name'		=> $formData['company_name'],
							'cui'		=> $formData['cui'],
							'reg_no'	=> $formData['reg_no']
						);
						$companyId = $modelUserCompany->saveCompany( $userCompany );
						//add address
						$modelAddress = new Application_Model_Addresses();
							$address = array(
								'deleted'			=> 0,
								'reference_type'	=> $_properties['references']['company'],
								'reference_id'		=> $companyId,
								'public_options'	=> ceil($formData['email_privacy'])+2*ceil($formData['phone_privacy'])+4*ceil($formData['address_privacy']),
								'phone'				=> $formData['phone'],
								'city_id'			=> $formData['city'],
								'address'			=> $formData['address'],
								'fax'				=> $formData['fax']
							);
						$modelAddress->saveAddress( $address );
					break;

					case 'farmacist':
						//save work experience
						if ( $formData['job'] == 0 ){
							$modelPharmacy = new Application_Model_Pharmacies();
							$pharma = array(
								'deleted'	=> 0,
								'name'		=> $formData['new_job']
							);
							$pharmaId = $modelPharmacy->savePharmacy( $pharma );
							// save address
							$modelAddress = new Application_Model_Addresses();
							$address = array(
								'deleted'			=> 0,
								'reference_type'	=> $_properties['references']['pharma'],
								'reference_id'		=> $pharmaId,
								'public_options'	=> ceil($formData['email_privacy'])+2*ceil($formData['phone_privacy'])+4*ceil($formData['address_privacy']),
								'city_id'			=> $formData['new_job_city'],
								'address'			=> $formData['new_job_address']
							);
							$modelAddress->saveAddress( $address );
						}
						else{
							$pharmaId = $formData['job'];
						}
						$modelUserWorkExperience = new Application_Model_UserWorkExperience();
						$workExperience = array(
							'user_id'=> $userID,
							'deleted'=> 0,
							'pharmacy_id'=> $pharmaId
						);
						$modelUserWorkExperience->saveWork( $workExperience );

					default:
						$modelUserDetails = new Application_Model_UsersDetails();
						$details = array(
							'user_id'		=> $userID,
							'first_name'	=> $formData['first_name'],
							'last_name'		=> $formData['last_name'],
							'specialization'=> $formData['specialization'],
							'grade'			=> $formData['grade'],
							'title'			=> $formData['title'],
							'gender'		=> $formData['gender'],
							'birth_date'	=> $formData['birth_date'],
							'source'		=> $formData['source']
						);
						$modelUserDetails->saveUserDetails($details);

						$modelAddress = new Application_Model_Addresses();
						$address = array(
							'deleted'			=> 0,
							'reference_type'	=> $_properties['references']['user'],
							'reference_id'		=> $userID,
							'public_options'	=> ceil($formData['email_privacy'])+2*ceil($formData['phone_privacy'])+4*ceil($formData['address_privacy']),
							'phone'				=> $formData['phone'],
							'email'				=> $formData['email'],
							'city_id'			=> $formData['city'],
							'address'			=> $formData['address']
						);
						$modelAddress->saveAddress( $address );
					break;
				}
				
				//redirect
				$this->_redirect('/users/register/pasul-trei');
			}
			$this->ns_register->step2 = $formData;
		}
		elseif ( !isset($this->ns_register->type) ){
			$this->_redirect('/users/register/');
		}
		
		$this->view->form = $form;
		$this->view->type = $this->ns_register->type;
    }

	public function pasulTreiAction()
    {

	}

}